﻿function showMore(element) {
    var index = $(element).attr('id');
    var data = {districtId: 1 ,pageIndex: index };

    $.ajax({
        type: "POST",
        url: "/District/ShowMoreRooms",
        content: "application/json; charset=utf-8",
        dataType: "json",
        data: data,
        success: function (d) {
            if (d.content != "") {
                $('.rooms-container').append(d.content);
                var increseIndex = parseInt(index) + 1;
                $('.show-more-rooms').attr('id', increseIndex);
            }
            else {
                $('.show-more-rooms').remove();
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            // TODO: Show error
        }
    });
}

function initialize(data) {
    var Location;
    var avgLat = 0;
    var avgLong = 0;
    var triangleCoords = new Array();
    $.each(data, function (i) {
        var point = new google.maps.LatLng(data[i].latitude, data[i].longitude);
        triangleCoords.push(point);
        avgLat += data[i].latitude;
        avgLong += data[i].longitude;
    });
    avgLat = avgLat / data.length;
    avgLong = avgLong / data.length;
    var mapOptions = {
        zoom: 16,
        center: new google.maps.LatLng(avgLat, avgLong),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);
    // Construct the polygon.
    Location = new google.maps.Polygon({
        paths: triangleCoords,
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35
    });

    Location.setMap(map);
}

$(document).ready(function () {

    //Get area location
    $.ajax({
        url: '/District/GetLocation',
        type: 'POST',
        content: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $.each(data, function (i) {
                initialize(data);
            });
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(xhr.responseText);
        }
    });

    $('.show-more-rooms').click(function (e) {
        e.preventDefault();
        showMore(this);
    });

    $(document).scroll(function (e) {
        if ($(window).scrollTop() + $(window).height() >= $(document).height()) {
            showMore($('.show-more-rooms'));
        }
    });


    $(".avaimg").live({
        mouseenter: function () {
            $(this).find(".detail").stop().fadeIn(500);
        },
        mouseleave: function () {
            $(this).find(".detail").stop().fadeOut(500);
        }
    });
});
